#!/bin/bash
_back_file(){
    cp -rf $target_directory/$target_file $backup_directory/$target_file-`date +%Y%m%d%H%M`
}

_clear_backfile(){
    time=`ls ./$backup_directory/ | grep -e "$target_file" | sed "s/$target_file-//g"`
    retain_time=`date +%Y%m%d%H%M -d "5 days ago"` 
    for file in ${time}
    do 
        if [ ${file} -lt ${retain_time} ] ;then
            remove=`ls ./$backup_directory/ | grep -e "$file"`
            rm -rf ./$backup_directory/$remove
        fi
    done
}

_deploy_file(){
    cp -rf $source_directory/$target_file $target_directory/$target_file
}

_scp_deploy_file(){
spawn scp -r $source_directory/$target_file $scp_user@$scp_ip:$target_directory/$target_file
expect {
"(yes/no)?"
{
send "yes\n"
expect "*assword:" { send "$passwd\r" }
}
"*assword:"
{
send "$passwd\r"
}
}
expect eof
EOF
}

_deploy_file
_scp_deploy_file
_scp_or_cp(){
    read -p "请选择传输方式（scp/cp）：" scp_or_cp
    if [ ${scp_or_cp} == "cp" ];then
        _clear_backfile
        _back_file
        _deploy_file
    elif [ ${scp_or_cp} == "scp" ];then
        _scp_deploy_file
    else
        echo -e "输入有误...\n----请确认输入的值为scp或者cp----"
    fi
}
main(){
#代码上传目录
target_directory="/var/appstatic/appstore/cosmic/cus"

#上传的文件名称
target_file="test.zip"

#代码包备份目录
backup_directory="./bakup"

#待部署的代码包目录
source_directory="./source"

scp_user="root"

scp_ip="192.168.1.20"




}

main "#@"
